﻿var id, url = "../TR/QuestionnaireSend.aspx",
    entity = "1";
var getUrl = "http://" + this.location.host + "/EditWidows/";
var src = "";
if (typeof ($) != 'undefined') {
    $(document).ready(function () {
        $("button").click(function (event) {
            if (event.target.id.indexOf("btn") > -1) {
                id = event.target.id;
                var name = event.target.name;
                var entityName;
                switch (name.toLowerCase()) {
                    case "a": //课前辅导
                        //entityName = "TrActivityCourse";
                        entityName = "V_TrActivityCourseExpectation";
                        postUrl(entityName, "", "TrActivityCourseID", "CourseName");
                        break;
                    case "b": // 培训课程
                        entityName = "TrCourse";
                        postUrl(entityName, "", "TrCourseID", "CourseName");
                        break;
                    case "c":  //培训客户
                        entityName = "V_TrCustomer";
                        postUrl(entityName, "", "TrCustomerId", "CustomerName");
                        break;
                    case "d": //部门
                        //内部客户
                        //entityName = "DatDepartment";
                        entityName = "V_DatDepartment";
                        postUrl(entityName, "", "DeptID", "DeptName");
                        break;
                    case "e": //职员
                        entityName = "DatEmployee";
                        postUrl(entityName, "", "EmpID", "Name");
                        break;
                    case "f": //后台所有客户
                        entityName = "V_CrmCustomerInfoSale";
                        postUrl(entityName, "", "ID", "CustomerName");
                        break;
                    case "fv": //价值业绩--缴款单已经结业的客户
                        entityName = "V_CrmCustomerInfoSale"; 
                        condition = "crmvalue";
                        postUrl(entityName, condition, "ID", "CustomerName");
                        break;
                    case "g": //业绩归属
                        entityName = "DatDepartment";
                        document.getElementById("EditUIWindow2").src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|False";
                        ShowDialog2(getSelected, id);
                        break;
                    case "h":
                        //选择客户总监
                        entityName = "V_UserEmp";
                        parent.document.getElementById("EditUIWindow1").src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&condition=" + decodeURI(condition);
                        parent.ShowDialog(getSelected, id);
                        break;
                    case "i":
                        //选择部门
                        entityName = "DatDepartment";
                        parent.document.getElementById("EditUIWindow1").src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|False";
                        parent.ShowDialog(getSelected, id);
                        break;
                    case "j":
                        //缴款单客户选择
                        entityName = "V_CrmPayByCid";
                        postUrl(entityName, "(CustomeerMgr='" + addUser + "' or LectorUser='" + addUser + "')", "ID", "Code");
                        break;
                    case "k":  //产品规划
                        entityName = "CrmProductLayout";
                        postUrl(entityName, "", "ID", "CourseLayoutName");
                        break;
                    case "l": //客户经理独立开发的客户
                        entityName = "V_CrmCustomerInfoSale";
                        condition = "crmlinkman";
                        postUrl(entityName, condition, "ID", "CustomerName");
                        break;
                    case "m":
                        //客户选择
                        entityName = "V_CrmCustomerShow";
                        postUrl(entityName, "", "ID", "CustomerName");
                        break;
                    case "mz":
                        //客户选择  --短信分组
                        entityName = "V_CrmCustomerShowM";
                        src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=DecisionTel&kname=CustomerName" + "&condition=" + decodeURI(condition);
                        ShowDialog(getSelected, id, src);
                        break;
                    case "n":
                        //流程授权用户选择
                        entityName = "V_UserEmp";
                        postUrl(entityName, "", "LoginName", "Name");
                        break;
                    case "o": //大课交付单选中
                        entityName = "V_DeliverOrderProduct";
                        postUrl(entityName, "", "ID", "OrderCode");
                        break;
                    case "p":  //职位
                        entityName = "DatPost";
                        src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatPost|PostName|PostID|ParentID|DeptID|False";
                        parent.ShowDialog(getSelected, id, src)
                        //postUrl(entityName, "", "PositionID", "PositionName");
                        break;
                    case "q": //订单
                        entityName = "V_CrmCustomerOrder";
                        postUrl(entityName, "", "ID", "Code");
                        break;
                    case "r":
                        var condition = "";
                        entityName = "V_UserEmp";  //选择外部用户
                        src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=UserIDs&kname=Name" + "&condition=" + decodeURI(condition);
                        ShowDialog1(getSelected, id, src);
                        break;
                    case "rv":
                        var condition = "";
                        entityName = "V_CrmValuePay";  //选择外部用户
                        src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=IDs&kname=Caption" + "&condition=" + decodeURI(condition);
                        ShowDialog1(getSelected, id, src);
                        break;
                    case "mes":
                        var condition = ""; 
                        var opt = document.getElementById("selectOption").value;
                        var entityName = "";
                        if (opt == "0")
                            entityName = "V_UserMessage";  //消息中心选择手机号
                        else if (opt == "1")
                            entityName = "V_CrmCustomerShowM";
                        src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=UserIDs&kname=Mobile" + "&condition=" + decodeURI(condition);
                        ShowDialog1(getSelected, id, src);
                        break;
                    case "s": //部门
                        entityName = "DatDepartment";
                        //debugger;
                        //                        if (typeof (editlist) == "undefined") {
                        //                            src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                        //                            parent.ShowDialog(getSelected, id, src)
                        //                        } else {
                        //                            src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                        //                            ShowDialog(getSelected, id, src)
                        //                        }
                        src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|False";
                        parent.ShowDialog(getSelected, id, src)
                        break;
                    case "t":
                        entityName = "DatUser";  //选择外部用户
                        var recordIDs = parent.mDataTable.getSelectedRows()[0];
                        records = parent.myDataTable.getRecord(recordIDs);
                        var t = records.getData("TrCustomerID");
                        postUrl(entityName, " CustomerID='" + t + "'", "UserID", "UName");
                        break;
                    case "u":    //选择用户
                        entityName = "DatUser";
                        postUrl(entityName, " isOutSite=0 and IsExpire=0", "UserID", "UName");
                        break;
                    case "v":
                        //选择客户经理
                        entityName = "V_UserEmp";
                        postUrl(entityName, "", "UserID", "Name");
                        break;
                    case "w":
                        //外部客户
                        entityName = "DatDepartment";
                        postUrl(entityName, "IsOutDepartment=1", "DeptID", "DeptName");
                        break;
                    case "x":
                        //缴款单用户选择
                        entityName = "V_UserEmp";
                        document.getElementById("EditUIWindow3").src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&kid=UserID&kname=Name&entity=" + entityName + "&condition=" + decodeURI(" isOutSite=0");
                        ShowDialog3(getSelected, id);
                        break;
                    case "x1":
                        //缴款单用户选择
                        entityName = "V_UserEmp";
                        parent.document.getElementById("EditUIWindow3").src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&kid=UserID&kname=Name&entity=" + entityName + "&condition=" + decodeURI(" isOutSite=0");
                        parent.ShowDialog3(getSelected, id);
                        break;
                    //客户现场选择                                                
                    case "y":
                        entityName = "V_CrmDeliverMajorProductLayout";
                        condition = "ProductLayOutId='" + StrProductId + "'";
                        postUrl(entityName, condition, "CustomerId", "CustomerName");
                        break;
                    case "z":
                        //用户选择
                        entityName = "V_UserEmp";
                        condition = decodeURI(" isOutSite=0 and IsExpire=0");
                        postUrl(entityName, condition, "UserID", "Name");
                        break;
                    case "emp": //选择职员
                        //debugger
                        //用户选择
                        entityName = "V_hrEmployee";
                        condition = "";
                        postUrl(entityName, condition, "ID", "CName");
                        break;
                    case "0":  //客户关系列表
                        entityName = "V_CrmCustomerRelation";
                        condition = "";
                        postUrl(entityName, condition, "ID", "CustomerName");
                        break;
                    case "1":  //大课交付申请单选择订单
                        entityName = "V_CrmCustomerInfoDeliver";
                        condition = "CustomeerMgr='" + addUser + "' and PhraseID='9b1639ba-d3fd-4f1c-bab2-12fedbc8b30d'";
                        postUrl(entityName, condition, "ID", "Code");
                        break;
                    case "2":  //项目交付申请单选择订单
                        entityName = "V_CrmCustomerInfoDeliver";
                        condition = "CustomeerMgr='" + addUser + "' and PhraseID='3AEDE3F6-1915-48D5-AFB2-C9380DB26813'";
                        postUrl(entityName, condition, "ID", "Code");
                        break;
                    case "3":  //内训交付申请单选择订单             
                        entityName = "V_CrmCustomerInfoDeliver";
                        //condition = "PhraseID='9b1639ba-d3fd-4f1c-bab2-12fedbc8b30d'";
                        condition = "CustomeerMgr='" + addUser + "' and PhraseID='9b1639ba-d3fd-4f1c-bab2-12fedbc8b30d' and ID in(select distinct orderid from V_DeliverOrderProduct where productid ='7DA3EF12-1094-4810-BB03-7DAC7AD66550') ";
                        postUrl(entityName, condition, "ID", "Code");
                        break;
                    case "4":  //内训交付申请单选择订单             
                        //condition = "PhraseID='9b1639ba-d3fd-4f1c-bab2-12fedbc8b30d'";
                        condition = "productid ='7DA3EF12-1094-4810-BB03-7DAC7AD66550' ";
                        entityName = "V_DeliverOrderProduct";
                        postUrl(entityName, condition, "ID", "OrderCode");
                        break;
                    case "20":  //英才交付申请单选择订单
                        entityName = "V_CrmCustomerInfoDeliver";
                        condition = "CustomeerMgr='" + addUser + "' and PhraseID='98FE5705-0780-4BF4-923C-F853E8FDC916'";
                        postUrl(entityName, condition, "ID", "Code");
                        break;
                    case "5":
                        var condition = "";
                        entityName = "V_UserEmp";  //选择多人    ,返回多个用户名 如zhangsan,lisi,wangwu
                        src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=LoginNames&kname=Name" + "&condition=" + decodeURI(condition);
                        parent.ShowDialog1(getSelected, id, src);
                        break;
                    case "6":
                        //单用户选择 ,返回单个用户名 如zhangsan
                        entityName = "V_UserEmp";
                        condition = decodeURI(" isOutSite=0 and IsExpire=0");
                        postUrl(entityName, condition, "LoginName", "Name");
                        break;
                    case "7":
                        //业绩归属 (选择分公司)
                        entityName = "DatDepartment";
                        condition = "deptType=1";
                        document.getElementById("EditUIWindow2").src = getUrl + "YUIMenuList.aspx?&condition=" + decodeURI(condition) + "&masterentity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|False";
                        ShowDialog2(getSelected, id);
                        break;
                    case "8": //客户切换(公海-客户信息)
                        entityName = "V_CrmCustomerShow";
                        document.getElementById("EditUIWindow1").src = getUrl + "SubjectList.aspx?entity=" + entityName + "&order=AddTime";
                        ShowDialog2(getSelected, id);
                        break;
                    case "9": //客户切换(客户信息-公海)
                        entityName = "V_CrmCustomerInfoSale";
                        document.getElementById("EditUIWindow1").src = getUrl + "SubjectList.aspx?entity=" + entityName + "&order=AddTime";
                        ShowDialog2(getSelected, id);
                        break;
                    case "pos": //调岗控件            
                        src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|False";
                        parent.ShowDialog(getSelectedAndDatposinit, id, src)
                        break;
                    case "dep": //跨门部门控件
                        entityName = "DatDepartment";
                        //                        if (typeof (editlist) == "undefined") {
                        //                            src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|False";
                        //                            parent.ShowDialog(getSelectedTRFDept, id, src)
                        //                        } else {
                        //                            src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                        //                            ShowDialog(getSelectedTRFDept, id, src)
                        //                        }
                        src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|False";
                        parent.ShowDialog(getSelectedTRFDept, id, src)
                        break;
                    case "muldep": //多部门选择
                        entityName = "DatDepartment";
                        document.getElementById("EditUIWindow2").src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|true";
                        ShowDialog2(getSelected, id);
                        break;

                }
                /*if (t == "M") {
                if (typeof (editlist) == "undefined") {
                parent.document.getElementById("EditUIWindow1").src = "../EditWidows/YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                parent.ShowDialog(getSelected, id)
                } else {
                document.getElementById("EditUIWindow1").src = "../EditWidows/YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                ShowDialog(getSelected, id)
                }
                }*/
                if (id.indexOf("btnDeptIds") > -1) {
                    entityName = "DatDepartment";
                    var src = "";
                    if (typeof (editlist) == "undefined") {
                        src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                        parent.ShowDialog(getSelected, id, src)
                    } else {
                        src = getUrl + "YUITree.aspx?r=" + Math.random() + "&entity=" + entityName + "&masterPara=DatDepartment|DeptName|DeptID|ParentID|DeptPath|True";
                        ShowDialog(getSelected, id, src)
                    }
                }
            }
        });
    });

    //提交传值 参数说明:  entityName-实体名称 condition-自定义条件 kid-值名称 kname-显示字段
    function postUrl(entityName, condition, kid, kname) {
        if (typeof (parent.ShowDialog) == "undefined") {
            src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=" + kid + "&kname=" + kname + "&condition=" + decodeURI(condition);
            ShowDialog(getSelected, id, src);
        }
        else {
            src = getUrl + "ChooseList.aspx?r=" + Math.random() + "&entity=" + entityName + "&kid=" + kid + "&kname=" + kname + "&condition=" + decodeURI(condition);
            parent.ShowDialog(getSelected, id, src);
        }
    }
    var list = new Array();
    function getSelected(v, t) {
        //alert(v);
        if (v != "") {
            if (typeof (v) == "object") {
                var str = v.join(',');
                $("#" + id.replace(/btn/g, "")).val(str);
                //YAHOO.util.Connect.asyncRequest('POST', url + "?flag=1&testjson=" + str + "&entity=" + entity, callback);

            }
            else {
                if (v.indexOf('@') == -1) {
                    $("#" + id.replace(/btn/g, "")).val(v);
                } else {
                    $("#DeptId").val(v.substring(0, 36));
                    $("#" + id.replace(/btn/g, "")).val(v.substring(37, v.length - 1));
                }
            }
            try {
                if (typeof (ValueChanged) == "function") {//值发生改变后的事件 ValueChanged
                    ValueChanged();
                }
            } catch (ex) {

            }
        }
        else {
            $("#" + id.replace(/btn/g, "")).val(v);
        }
        $("#txt" + id.replace(/btn/g, "")).val(t);
        try {
            if (typeof (TextChanged) == "function" && $("#txt" + id.replace(/btn/g, "")).val() != "") {//文本发生改变后的事件 ValueChanged
                TextChanged();
            }
        } catch (ex) {

        }
    }

    function getSelectedTRFDept(v, t) {
        //alert(v);
        //debugger;
        if (v != "") {
            if (typeof (v) == "object") {
                var str = v.join(',');
                $("#" + id.replace(/btn/g, "")).val(str);
                $("#TRF" + id.replace(/btn/g, "")).val(str);
                //YAHOO.util.Connect.asyncRequest('POST', url + "?flag=1&testjson=" + str + "&entity=" + entity, callback);

            }
            else {
                if (v.indexOf('@') == -1) {
                    $("#" + id.replace(/btn/g, "")).val(v);
                    $("#TRF" + id.replace(/btn/g, "")).val(v);
                } else {
                    $("#TRFDeptID").val(v.substring(0, 36));
                    $("#TRF" + id.replace(/btn/g, "")).val(v.substring(37, v.length - 1));
                }
            }
        }
        $("#txt" + id.replace(/btn/g, "")).val(t);
    }
    function getSelectedAndDatposinit(v, t) {
        //alert(v);
        var selectValue = "";
        if (v != "") {
            if (typeof (v) == "object") {
                var str = v.join(',');
                $("#" + id.replace(/btn/g, "")).val(str);
                selectValue = str;
               // YAHOO.util.Connect.asyncRequest('POST', url + "?flag=1&testjson=" + str + "&entity=" + entity, callback);

            }
            else {
                if (v.indexOf('@') == -1) {
                    $("#" + id.replace(/btn/g, "")).val(v);
                    selectValue = v;
                } else {
                    $("#DeptId").val(v.substring(0, 36));
                    $("#" + id.replace(/btn/g, "")).val(v.substring(37, v.length - 1));
                    selectValue = v.substring(37, v.length - 1);
                }
            }
        }
        $("#txt" + id.replace(/btn/g, "")).val(t);
        if (selectValue != "")
        {
            $("#TRFDeptID").val(selectValue);
            $("#TRFDeptName").val(t);
            //getDatPost(selectValue, "#" + id.replace(/btn/g, ""));
            getDatPost(selectValue, "#TRFPostID");
            
        }
    }



    function DatPost_Change(obj) {
      
      $("#TRFPostName").val(obj.options[obj.options.selectedIndex].text);
    }

    function getDatPost(deptID, ctrlName) {
        var url = "../Extend/AjaxData/jsonlist.aspx?entity=DatPost&condition=DeptID='" + deptID + "'";
        $.get(url, function (d, s) {
            // if (d.indexOf("") == -1) {
            var list = eval("(" + d + ")");
            if (list.total == 0) {
                alert("该部门无岗位信息，请先维护部门岗位信息！");
                //parent.parent.closeWin();
            }
            else {
                //var ctrlName = "#PositionID";

                var select = $(ctrlName);
                //select[0].options
                select[0].options.length = 0;
                select[0].options.add(new Option('', ''));

                for (var o in list.rows) {
                    var v = list.rows[o].PostID;
                    var t = list.rows[o].PostName;
                    //alert(t);
                    select[0].options.add(new Option(t, v));
                    //additemtoselect(select,t,v);
                }
                
  

            }
            // }
        })
    }
    function additemtoselect(objselect, objitemtext, objitemvalue) {
        var varitem = new Option(objitemtext, objitemvalue);
        objselect.options.add(varitem);
    }
    var responseSuccess = function (res) {
        try {
            var str = res.responseText;
            var obj = eval(str);

            var items = eval("[" + str + "]");
            var userIDs = [], userNames = [];
            for (var q = 0; q < items.length; q++) {
                for (var i = 0; i < items[q].length; i++) {
                    userIDs.push(items[q][i].UserID);
                    userNames.push(items[q][i].UName);
                }
            }
            var left = document.getElementById("lstBoxSubject");
            //left.remove(0);
            var l = left.length;
            for (var x = 0; x < l; x++) {
                left.remove(0);
            }
            for (var j = 0; j < userIDs.length; j++) {
                left.add(new Option(userNames[j], userIDs[j])); //
            }
            //alert(obj.length);
            if (str == "1") {
                alert("数据保存成功");
                if (action == 'insert') {
                    //parent.fnAddRow(instance[0]);
                    parent.ReLoadData();
                } else {
                    parent.ReLoadData();
                    //parent.fnUpdateRow(instance[0]);
                }
                parent.closeWin();
            }
            else {
                showMessage();
                alert(str + "\n" + "数据保存失败!");
            }
        }
        catch (e) {
        }
        finally {
        }
    };
    var responseFailure = function (error) {
        alert("数据错误：" + error.statusText);
    }
    // Create a callback object that defines success and failure
    // handlers, and instructs Connection Manager to use the
    // Flash transport by defining the xdr property and setting
    // it to true.
    var callback = {
        success: responseSuccess,
        failure: responseFailure
    };
    function test(v, t) {
        alert("1");
    }
}

//将document.ready中的选择职员代码移至方法中
var SelectEmployee = function () {
    $("button").click(function (event) {
        if (event.target.id.indexOf("btn") > -1) {
            id = event.target.id;
            var name = event.target.name;
            var entityName;
            switch (name.toLowerCase()) {
                case "emp": //选择职员
                    //用户选择
                    entityName = "V_hrEmployee";
                    condition = "";
                    postUrl(entityName, condition, "ID", "CName");
                    break;      
            }
        }
    });
}